<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="故障ID" prop="faultId">
        <el-input
            v-model="queryParams.faultId"
            placeholder="请输入故障ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
                        v-model="queryParams.startTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="维修人员" prop="technician">
        <el-input
            v-model="queryParams.technician"
            placeholder="请输入维修人员"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用" prop="cost">
        <el-input
            v-model="queryParams.cost"
            placeholder="请输入费用"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['fixed_record:fixed_record:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['fixed_record:fixed_record:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['fixed_record:fixed_record:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['fixed_record:fixed_record:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fixed_recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" align="center" prop="maintenanceId"/>
      <el-table-column label="故障设备" align="center" width="200px">
        <template #default="scope">
          {{ scope.row.location }}·{{ scope.row.model }}
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修人员" align="center" prop="technician"/>
      <el-table-column label="维修措施" align="center" prop="actionsTaken"/>
      <el-table-column label="费用" align="center" prop="cost"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['fixed_record:fixed_record:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['fixed_record:fixed_record:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改维修记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="fixed_recordRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="故障设备" prop="faultId">
          <el-select v-model="form.faultId" placeholder="请选择故障设备">
            <el-option v-for="item in faultRecordList" :key="item.faultId"
                       :label="`${parseTime(item.faultTime, '{y}-{m}-{d}')}·${item.location}·${item.model}`"
                       :value="item.faultId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
                          v-model="form.startTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
                          v-model="form.endTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维修人员" prop="technician">
          <el-input v-model="form.technician" placeholder="请输入维修人员"/>
        </el-form-item>
        <el-form-item label="维修措施" prop="actionsTaken">
          <el-input v-model="form.actionsTaken" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="费用" prop="cost">
          <el-input v-model="form.cost" placeholder="请输入费用"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Fixed_record">
import {
  listFixed_record,
  getFixed_record,
  delFixed_record,
  addFixed_record,
  updateFixed_record
} from "@/api/fixed_record/fixed_record";
import {listFault_record} from "@/api/fault_record/fault_record.js";

const {proxy} = getCurrentInstance();

const fixed_recordList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const faultRecordList = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    faultId: null,
    startTime: null,
    endTime: null,
    technician: null,
    actionsTaken: null,
    cost: null,
  },
  rules: {
    faultId: [
      {required: true, message: "故障ID不能为空", trigger: "blur"}
    ],
    startTime: [
      {required: true, message: "开始时间不能为空", trigger: "blur"}
    ],
    endTime: [
      {required: true, message: "结束时间不能为空", trigger: "blur"}
    ],
    technician: [
      {required: true, message: "维修人员不能为空", trigger: "blur"}
    ],
    actionsTaken: [
      {required: true, message: "维修措施不能为空", trigger: "blur"}
    ],
    cost: [
      {required: true, message: "费用不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询维修记录列表 */
function getList() {
  loading.value = true;
  listFixed_record(queryParams.value).then(response => {
    fixed_recordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 新增/修改时查询故障列表
function getFaultRecordList() {
  listFault_record().then(response => {
    faultRecordList.value = response.rows;
    faultRecordList.value.sort((a, b) => new Date(b.faultTime) - new Date(a.faultTime));
  });

}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    maintenanceId: null,
    faultId: null,
    startTime: null,
    endTime: null,
    technician: null,
    actionsTaken: null,
    cost: null,
    createTime: null,
    updateTime: null,
    updateBy: null,
    remark: null,
    createBy: null
  };
  proxy.resetForm("fixed_recordRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.maintenanceId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加维修记录";
  getFaultRecordList();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _maintenanceId = row.maintenanceId || ids.value
  getFixed_record(_maintenanceId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改维修记录";
  });
  getFaultRecordList();
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["fixed_recordRef"].validate(valid => {
    if (valid) {
      if (form.value.maintenanceId != null) {
        updateFixed_record(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFixed_record(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _maintenanceIds = row.maintenanceId || ids.value;
  proxy.$modal.confirm('是否确认删除维修记录编号为"' + _maintenanceIds + '"的数据项？').then(function () {
    return delFixed_record(_maintenanceIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('fixed_record/fixed_record/export', {
    ...queryParams.value
  }, `fixed_record_${new Date().getTime()}.xlsx`)
}

getList();
</script>
